import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'

// 获取当前文件的目录
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

// 读取config.json文件
function readOriginConfig() {
  console.log('🔍 开始读取 config.json...')
  const configPath = path.join(__dirname, '../src/config.json')
  console.log('📂 配置文件路径:', configPath)

  if (!fs.existsSync(configPath)) {
    throw new Error(`文件不存在: ${configPath}`)
  }

  const configContent = fs.readFileSync(configPath, 'utf8')
  console.log('📄 文件内容长度:', configContent.length)

  try {
    const originAssistant = JSON.parse(configContent)
    console.log('📖 读取到 config.json 配置:', originAssistant)
    return originAssistant
  } catch (error) {
    console.error('❌ JSON 解析错误:', error.message)
    console.error('📄 文件内容:', configContent)
    throw new Error(`解析 config.json 失败: ${error.message}`)
  }
}

// 扫描目录结构
function scanDirectoryStructure(assistantKey) {
  const publicPath = path.join(__dirname, '../public', assistantKey)

  if (!fs.existsSync(publicPath)) {
    console.warn(`警告: ${assistantKey} 目录不存在`)
    return null
  }

  const structure = {
    thumbnail: null,
    tableKey: [],
    tableList: {},
    contentList: [],
  }

  // 查找缩略图
  const thumbnailPath = path.join(publicPath, 'Thumbnail')
  if (fs.existsSync(thumbnailPath)) {
    const thumbnailFiles = fs.readdirSync(thumbnailPath)
    if (thumbnailFiles.length > 0) {
      structure.thumbnail = `/${assistantKey}/Thumbnail/${thumbnailFiles[0]}`
    }
  }

  // 扫描平台目录 (Android, iOS, PC)
  const platforms = ['Android', 'iOS', 'PC']

  platforms.forEach((platform) => {
    const platformPath = path.join(publicPath, platform)
    if (fs.existsSync(platformPath)) {
      structure.tableKey.push(platform)
      structure.tableList[platform] = []

      // 扫描步骤目录
      const stepDirs = fs
        .readdirSync(platformPath)
        .filter((dir) =>
          fs.statSync(path.join(platformPath, dir)).isDirectory(),
        )
        .sort((a, b) => {
          // 按步骤名称排序 (Step_1, Step_2, etc.)
          const aNum = parseInt(a.match(/Step_(\d+)/)?.[1] || '0')
          const bNum = parseInt(b.match(/Step_(\d+)/)?.[1] || '0')
          return aNum - bNum
        })

      stepDirs.forEach((stepDir) => {
        const stepPath = path.join(platformPath, stepDir)
        let mediaFile = null
        let articleFile = null

        // 查找所有文件，按文件名区分
        const files = fs.readdirSync(stepPath)
        files.forEach((file) => {
          const lower = file.toLowerCase()
          if (lower.includes('media')) {
            mediaFile = `/${assistantKey}/${platform}/${stepDir}/${file}`
          } else if (lower.includes('article')) {
            articleFile = `/${assistantKey}/${platform}/${stepDir}/${file}`
          }
        })

        if (mediaFile && articleFile) {
          const stepData = {
            media: mediaFile,
            article: articleFile,
          }

          structure.tableList[platform].push(stepData)
          structure.contentList.push(stepData)
        } else {
          console.warn(
            `警告: ${assistantKey}/${platform}/${stepDir} 缺少媒体文件或文章文件`,
          )
        }
      })
    }
  })

  return structure
}

// 生成AssistantList配置
function generateAssistantList(originAssistant) {
  const assistantList = []

  originAssistant.forEach((assistant) => {
    console.log(`🔍 扫描 ${assistant.key} 目录...`)
    const structure = scanDirectoryStructure(assistant.key)

    if (structure) {
      const assistantData = {
        key: assistant.key,
        title: assistant.title,
        introduction: assistant.introduction,
        thumbnail: structure.thumbnail,
        tableKey: structure.tableKey,
        tableList: structure.tableList,
        contentList: structure.contentList,
      }

      assistantList.push(assistantData)
      console.log(`✅ ${assistant.key} 配置生成完成`)
    } else {
      console.warn(`⚠️ 跳过 ${assistant.key}，目录结构不完整`)
    }
  })

  return assistantList
}

// 生成config.ts文件
function writeConfigTs(originAssistant, assistantList) {
  const configPath = path.join(__dirname, '../src/config.ts')

  // 生成 TypeScript 格式字符串
  const toTypeScriptString = (obj, indent = 2) => {
    const jsonStr = JSON.stringify(obj, null, indent)
    return jsonStr
      .replace(/"([^"]+)":/g, '$1:') // 移除属性名的引号
      .replace(/"/g, "'") // 将字符串值的双引号改为单引号
  }

  const fileContent = `// 该文件由脚本自动生成，请勿手动修改
import type { OriginAssistantType, AssistantProps } from '@/type'

export const OriginAssistant: OriginAssistantType[] = ${toTypeScriptString(originAssistant)}

export const AssistantList: AssistantProps[] = ${toTypeScriptString(assistantList)}
`

  // 备份原文件（如果存在）
  if (fs.existsSync(configPath)) {
    const backupPath = configPath + '.backup'
    fs.writeFileSync(backupPath, fs.readFileSync(configPath, 'utf8'), 'utf8')
    console.log(`📁 原文件已备份到: ${backupPath}`)
  }

  fs.writeFileSync(configPath, fileContent, 'utf8')
  console.log('✅ config.ts 文件已重新生成')
}

// 主函数
function main() {
  try {
    console.log('🚀 开始生成AssistantList...')
    console.log('📂 当前工作目录:', process.cwd())

    // 读取OriginAssistant配置
    const originAssistant = readOriginConfig()
    console.log(`📖 读取到 ${originAssistant.length} 个助手配置`)

    // 生成AssistantList
    const assistantList = generateAssistantList(originAssistant)
    console.log(`✅ 生成了 ${assistantList.length} 个助手列表项`)

    if (assistantList.length === 0) {
      console.warn('⚠️ 没有生成任何助手列表项，请检查目录结构')
      return
    }

    // 生成config.ts文件
    writeConfigTs(originAssistant, assistantList)

    console.log('🎉 AssistantList生成完成！')

    // 输出生成的配置预览
    console.log('\n📋 生成的配置预览:')
    assistantList.forEach((assistant, index) => {
      console.log(`${index + 1}. ${assistant.key}: ${assistant.title}`)
      console.log(`   平台: ${assistant.tableKey.join(', ')}`)
      console.log(`   步骤数: ${assistant.contentList.length}`)
      if (assistant.thumbnail) {
        console.log(`   缩略图: ${assistant.thumbnail}`)
      }
    })
  } catch (error) {
    console.error('❌ 生成失败:', error.message)
    console.error(error.stack)
    process.exit(1)
  }
}

// 如果直接运行此脚本,执行main函数
main()

export { scanDirectoryStructure, generateAssistantList, writeConfigTs }
